package com.zline.foodraise.js;

import android.webkit.WebView;

import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.AppPref_;
import com.zline.foodraise.listener.JsInvokeListener;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.view.dialog.LoadingProgressDialog;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.json.JSONObject;

import cn.pedant.SafeWebViewBridge.JsCallback;

/**
 * Created by heshantao on 15/7/16.
 * JS 操作注入类
 */@EBean(scope = EBean.Scope.Singleton)
public class HostJSScope {
    private static final String LOG_TAG = HostJSScope.class.getSimpleName();
    static WebView wv;
    @Pref
    static AppPref_ appPref;
    static JsInvokeListener jsListener;
    static LoadingProgressDialog dialog;

    //延迟执行
    public static void delayJsCallBack(WebView webView, int ms, final String msg, final JsCallback jsCallback) {
        TaskExecutor.scheduleTaskOnUiThread(ms * 1000, new Runnable() {
            @Override
            public void run() {
                try {
                    jsCallback.apply(msg);
                } catch (JsCallback.JsCallbackException je) {
                    je.printStackTrace();
                }
            }
        });
    }

    public static void setJsInvokeListener(JsInvokeListener listener) {
        jsListener = listener;
    }

    //设置地址
    public static void setAddress(WebView webView) {
        LUtil.i(LOG_TAG, "______setAddress_______");
        jsListener.jsInvoke(Constant.WEB_SET_ADDR);
    }

    //显示其他人profile页
    public static void showUserProfile(WebView webView, JSONObject object) {
        LUtil.i(LOG_TAG, "______Project_showProfile_______"+object);
        jsListener.jsInvoke(Constant.WEB_PROFILE_FLAG, object);
    }


    //请求筹用户关注列表
    public static void showUserList(WebView webView, JSONObject object) {
        LUtil.i(LOG_TAG, "______Project_showUserList_______"+object);
        jsListener.jsInvoke(Constant.WEB_ATTENTION_FLAG, object);
    }

    //重新加载刷新页面
    public static void reload(WebView webView) {
        LUtil.i(LOG_TAG, "______reload_______");
        webView.reload();
    }


    //请求评论列表
    public static void showCommentList(WebView webView, JSONObject object) {
        LUtil.i(LOG_TAG, "______Project_showCommentList_______");
        jsListener.jsInvoke(Constant.WEB_COMMENT_LIST_FLAG, object);
    }

    //请求添加评论
    public static void addComment(WebView webView, JSONObject object) {
        LUtil.i(LOG_TAG, "_______addComment_______");
        jsListener.jsInvoke(Constant.WEB_ADD_COMMENT_FLAG,object);
    }

    //请求登录接口
    public static void showLogin(WebView webView) {
        LUtil.i(LOG_TAG, "______showLogin_______");
        jsListener.jsInvoke(Constant.WEB_LOGIN);

    }

    //请求支付
    public static void pay(WebView webView,JSONObject jsonObject) {
        LUtil.i(LOG_TAG, "______pay_______"+jsonObject.toString());
        jsListener.jsInvoke(Constant.WEB_PAY_FLAG, jsonObject);
    }


    //收货地址设置
    public static void jumpPrePage(WebView webView,JSONObject jsonObject) {
        LUtil.i(LOG_TAG, "______jumpPrePage_______" + jsonObject.toString());
        jsListener.jsInvoke(Constant.WEB_JUMP_LAST_PAGE,jsonObject);
    }


    //筹、吃详情页，滚动距离上边界距离
    public static void scrollTop(WebView webView,double top) {
        LUtil.i(LOG_TAG, "______scrollTop_______" + top);
        jsListener.jsInvoke(top);

    }

    //获得分享信息
    public static void snsShare(WebView webView,JSONObject jsonObject) {
        LUtil.i(LOG_TAG, "______snsShare_______" + jsonObject.toString());
        jsListener.jsInvoke(Constant.WEB_SHARE_FLAG,jsonObject);
    }

    //跳转筹列表页
    public static void jumpCfHome(WebView webView) {
        LUtil.i(LOG_TAG, "______jumpCfHome_______" );
        jsListener.jsInvoke(Constant.WEB_JUMP_HOME_PAGE);
    }


    //跳转吃列表页
    public static void jumpCcHome(WebView webView) {
        LUtil.i(LOG_TAG, "______jumpCcHome_______" );
        jsListener.jsInvoke(Constant.WEB_JUMP_EAT_PAGE);
    }


    //js 请求基本信息
    public static void getUserInfo(WebView webView) {
        AppPref_ pref = new AppPref_(webView.getContext());
        long cityId = pref.cityId().get();
        double latPoint = Double.parseDouble(pref.latPoint().get());
        double lonPoint = Double.parseDouble(pref.lonPoint().get());
        String userAddress = pref.userAddress().get();
        String cityName=pref.city().get();
        long userId = pref.userId().get();
        String token = pref.token().get();
        LUtil.i(LOG_TAG, "__Home_getUserInfo_______" + latPoint + "____lonPoint____" + lonPoint+"_cityId__"+cityId);
        webView.loadUrl("javascript:getUserInfo('" + latPoint + "','" + lonPoint + "','" + userId + "','" + cityId +
                "','" + cityName + "','" + userAddress + "','" + token + "')");
    }

    //显示加载对话框
    public static void showLoading(WebView webView){
        LUtil.i(LOG_TAG,"________showLoading______");
        if(dialog==null){
            dialog=LoadingProgressDialog.init(webView.getContext());
            dialog.show();
        }
    }

    //取消加载对话框
    public static void hideLoading(WebView webView){
        LUtil.i(LOG_TAG,"________hideLoading______");
        if(dialog!=null){
            dialog.dismiss();
            dialog=null;
        }
    }

    //获得对筹的关注状态
    public static void showFollowBtn(WebView webView,JSONObject jsonObject) {
        jsListener.jsInvoke(Constant.WEB_PROJECT_ATTENTION, jsonObject);
    }

    //获得预约状态，和收货地址是否改变
    public static void saveSendTypeAndAppointment(WebView webView,JSONObject jsonObject){
        LUtil.i(LOG_TAG,"___________SVAE_____"+jsonObject.toString());
        jsListener.jsInvoke(Constant.WEB_SENDTYPE_APPOINTMENT, jsonObject);

    }


}

